package alo.p10;

/**
 * @ClassName BubbleSort
 * @Description 冒泡排序
 * @Author Fanxc
 * @DATE 2021/3/11 11:42
 * @Version 1.0
 */
public class BubbleSort {
    void bubbleSort(int[] A,int n){
        int temp;
        for (int pass=n-1;pass>=0;pass--){
            for (int i = 0; i < pass - 1; i++) {
                if (A[i]>A[i+1]){
                    temp=A[i];
                    A[i]=A[i+1];
                    A[i+1]=temp;
                }
            }
        }
    }

    void bubbleSortImproved(int[] A,int n){
        int temp;
        boolean swapped=true;
        for (int pass=n-1;pass>=0&&swapped;pass--){
            swapped=false;
            for (int i = 0; i < pass - 1; i++) {
                if (A[i]>A[i+1]){
                    temp=A[i];
                    A[i]=A[i+1];
                    A[i+1]=temp;
                    swapped=false;
                }
            }
        }
    }
}
